package net.sky.oa.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sky.oa.dao.asms.AsmsDao;
import net.sky.oa.dao.asms.impl.AsmsDaoImpl;
import net.sky.oa.model.SmsEmployee;
import net.sky.oa.model.SmsInfo;
import net.sky.oa.model.SmsRecieve;
import net.sky.oa.model.SysEmployee;
import net.sky.oa.service.system.SysEmployeeService;

public class SmsUtil {

	public  static void setSmsEmployee(SmsEmployee smsEmployee,String content,int people) {
		if(null == smsEmployee){
			smsEmployee = new SmsEmployee();
		}
			
		int conLength = content.length();
		int smsCount = getSmsCount(conLength)*people;
		smsEmployee.setWords(conLength);
		smsEmployee.setCount(smsCount);
		smsEmployee.setAmount(smsCount*0.05);
		
	}
	
	/*
	 * 根据短信内容计算短信条数
	 */
	public static int getSmsCount(int conLength) {
		int count = 0;
		
		if(conLength<=60) {
			count = 1;
		} else if(conLength>60&&conLength<=67) {
			count = 2;
		} else {
			count = ((conLength+10)+66)/67;
		}
		return count;
	}
	
	
	public static List<String> getUserMobile(String userIds) {
		String userArray[] = userIds.split(",");
		return null;
	}
	
	/*
	 * 根据员工List 获取手机号码
	 */
	public static Map getMobile(List<SysEmployee> userList) {	
		Map map = new HashMap();
		
		String str = "";
		boolean flag = false;
		
		String mobile = "";
		//验证电话号码
		for(SysEmployee user:userList) {
			if(null==user.getMobile()||"".equals(user.getMobile())) {
				str = user.getName()+"手机号码为空!";
				map.put("success", "false");
				map.put("error", str);
				return map;
			}
			flag = RegexpUtil.isHardRegexpValidate(user.getMobile(), RegexpUtil.mobile_regexp);
			if(false == flag) {
				str = user.getName()+"-->"+user.getMobile()+"手机号码有误 !";
				map.put("success", "false");
				map.put("error", str);		
				return map;
				
			} else {
				mobile = mobile+user.getMobile()+",";
			}
		}
		
		map.put("success", "true");
		map.put("result", mobile);
		
		return map;
	}
	 
	
	/*
	 * 拆分'学员,,短信'List集合抓取手机号码 
	 * 手机号码验证
	 */
	public static Map getStuMobiles(List<String> mobileList) {
		Map map = new HashMap();
		String str = "";
		boolean flag = false;		
		String mobiles = "";
		String infNames = "";
		//验证手机号码
		for(String mobile:mobileList) {			
			String[] mobileArr = mobile.split(",,");			
			if(mobileArr.length<2) {
				str = mobileArr[1]+"手机号码为空!";
				map.put("success", "false");
				map.put("error", str);
				return map;
			}
			
			flag = RegexpUtil.isHardRegexpValidate(mobileArr[0], RegexpUtil.mobile_regexp);
			if(false == flag) {
				str = mobileArr[1]+"-->"+mobileArr[0]+"手机号码有误 !";
				map.put("success", "false");
				map.put("error", str);		
				return map;
				
			} else {
				mobiles = mobiles+mobileArr[0]+",";
				infNames = infNames + mobileArr[1]+",";
			}
			
		}
		map.put("success", "true");
		map.put("result", mobiles);
		map.put("infNames", infNames);
		
		return map;
	}
	
	/*
	 * 批量发送短信
	 */
	public static int  sendMsg(SmsInfo smsInfo,String content,String mobiles) {
		int result = 0;
		//result = MsgOperate.sendMsg(smsInfo.getAccount(), smsInfo.getPassword(), content, mobiles);		
		result = MsgOperate.sendMsg(smsInfo.getAccount(), smsInfo.getPassword(), content, mobiles);		
		return result;
	}
	
	/*
	 * 接收回复信息  组装List<SmsRecieve>后返回
	 */
	public List<SmsRecieve> recieveMsg(SmsInfo smsInfo,SysEmployeeService employeeService) {		
		List<SmsRecieve> list = new ArrayList<SmsRecieve>();
		String[] arrStr = MsgOperate.recieveMsg(smsInfo.getAccount(),smsInfo.getPassword());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(null!=arrStr) {
			AsmsDao asmsDao = new AsmsDaoImpl();
			for(int i=0;i<arrStr.length;i++) {
				String str = arrStr[i];
				if("OK,0".equals(str)||"ER,-300".equals(str)||str.length()<10) {
					return list;
				} else{
					SmsRecieve smsRecieve = new SmsRecieve();
					smsRecieve.setAccount(smsInfo.getAccount());
					smsRecieve.setPort(smsInfo.getPort());
					smsRecieve.setStatus(1);
					smsRecieve.setRecievetime(new Date());
					smsRecieve.setUser(smsInfo.getUser());
					
					int index = str.indexOf(",*,");				
					
					String[] msgInfo = str.substring(0,index).split(",");					
					try {
						smsRecieve.setReplytime(sdf.parse(msgInfo[0]+" "+msgInfo[1]));
						smsRecieve.setMobile(msgInfo[2]);
						
						//查找学员姓名
						String stuName = asmsDao.findStuNameByMobile(msgInfo[2]);
						if(null==stuName||"".equals(stuName)) {
							//调用OA查看 员工姓名
							List<SysEmployee> empList=employeeService.findByMobile(msgInfo[2]);
							if(null!=empList){
								stuName=empList.get(0).getName();
							}
							empList=null;
						}
						smsRecieve.setReplier(stuName);
					} catch (Exception e) {						
						e.printStackTrace();
					}
					
					String msgContent = str.substring(index+3);
					if(i==arrStr.length-1) {
						//msgContent = msgContent.substring(0,msgContent.length()-1);通道之前回复尾部会多一个字符，现已不会 
						msgContent = msgContent.substring(0,msgContent.length()); 
					} 
					smsRecieve.setContent(msgContent);
					
					list.add(smsRecieve);
				}
				
			}
		} 
		return list;
	}
	
	/*
	 * 短信发送失败异常信息转换
	 */
	public static String smsResultToString(int i) {
		String result = "";
		if(i>0) {
			return result;
		}
		switch(i) {
			//发送异常信息
			case -2:
				result =  "电话号码个数超过1000!";
				break;
			case -11:
				result = "电话号码中有非数字字符!";
				break;
			case -12:
				result = "有异常电话号码!";
			case -101:
				result = "发送消息等待超时!";
				break;
			case -102:
				result = "发送或接收消息失败!";
				break;
			case -103:
				result = "接收消息超时!";
				break;
			
			
			//服务器异常信息
			case -10001:
				result = "短信账号信息设置有误!";
				break;
			case -10003:
				result = "短信账号余额不足!";
				break;
			case -10004:
				result = "手机号码不正确!";
				break;
			case -10011:
				result = "信息内容超长!";
				break;
			case -10015:
				result = "服务器繁忙!";
				break;
			case -10033:
				result = "短信内容含有非法关键字!";
				break;				
			default:
				result = "短信出现异常!";
			
		}
		return result;
	}
}
